﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data.SqlClient;
using UtilsBase;

using Microsoft.AspNetCore.Http;

using System.Web;

using ServiceWeb.WebAdmin;
using AngleSharp.Dom;
using Models.Common;
using Models.JsonResultModel;
using UtilsORM;
using UtilsORM.MySqlData;
using Microsoft.EntityFrameworkCore;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class MagerMainController : BaseMagerController
    {

        // GET: MagerMain
        public IActionResult Index()
        {
            // new AutoTask_Content().CreateMenuCount();


            //            string sql = @"select (select pty_name from [dbo].[PM_Property] where pty_guid=empd_com_guid)+
            //(select pty_name from[dbo].[PM_Property] where pty_guid = empd_project_guid) +
            //(select dep_name from[dbo].PM_Department where dep_guid = empd_dep_guid)
            // UserInfo,emp_name
            // from PM_Employee inner join [dbo].[PM_Employee_Dep] on empd_emp_guid = emp_guid  and empd_type = 1 where emp_guid='" + base.LoginUserInfo.Emp_Guid + "'  ";
            //DataTable dt = new UtilsORM.DataBase().QueryDt(sql);
            //if (dt.Rows.Count > 0)
            //{
            ViewData["Emp_Name"] = LoginUserInfo.user_Name;
            //    ViewData["Emp_Dep"] = dt.Rows[0][0].ToString();
            //}
            //dt.Dispose();

            //读取菜单
            DataTable DtMenuList = new ServiceSystemMenu().GetList(Guid.Empty, LoginUserInfo);

            ViewData["DataList"] = DtMenuList;
            return View();
        }
        public IActionResult Main(string menuguid)
        {

            ViewData["LoginUserInfo"] = LoginUserInfo;

            ViewBag.URL = "/MagerMain/right";

            //读取当前主菜单名称        
            ViewBag.PMenuName = new ServiceSystemMenu().GetMenuName(menuguid);

            //读取菜单            
            DataTable DtMenuList = new ServiceSystemMenu().GetList(new Guid(menuguid), LoginUserInfo);

            DataRow[] dr = DtMenuList.Select(" menu_index=1");
            if (dr.Count() > 0)
            {
                ViewBag.URL = dr[0]["menu_LinkUrl"].ToString();
            }
            ViewData["DataList"] = DtMenuList;
            return View();
        }

        public IActionResult ListDialog()
        {

            return View();
        }

        //public IActionResult LogOut()
        //{
        //    CookieSessionHelper.DeleteCookies(HttpContext, "logintoken");
        //    ///退出登录

        //    string url = Configini.Configini.GetDicSystemConfig("mywebsite_loginurl");

        //    return Redirect(url);// ("Login", "MagerLogin");
        //}



        [HttpPost]
        public RespResultData SavePwd(IFormCollection FC)
        {
            #region      
            if (FC.Count > 0)
            {

                string oldpwd = UtilsDecrypt.DoMd5Encode(FC["oldpwd"].ToString().Trim(), "32"); //new BaseClass.StringQuery().DoMd5Encode(new BaseClass.StringQuery().GetString(FC["txt_oldpwd"].Trim()), "32");
                string newpwd = UtilsDecrypt.DoMd5Encode(FC["newpwd"].ToString().Trim(), "32"); //new BaseClass.StringQuery().DoMd5Encode(new BaseClass.StringQuery().GetString(FC["txt_newpwd"].Trim()), "32"); //int.Parse(FC["txt_newpwd"].ToString().Trim());
                string newpwd2 = UtilsDecrypt.DoMd5Encode(FC["newpwd2"].ToString().Trim(), "32");// new BaseClass.StringQuery().DoMd5Encode(new BaseClass.StringQuery().GetString(FC["txt_newpwd2"].Trim()), "32"); //int.Parse(FC["txt_newpwd2"].ToString().Trim());

                EFYihongIotContext EFYihongIotContext = new EFYihongIotContext();

                bs_client_user user = EFYihongIotContext.bs_client_user.Where(c => c.cu_guid == LoginUserInfo.user_guid).FirstOrDefault();
                if (user == null)
                {
                    return RespResult.Faild("用户无效");
                }
                if (user.cu_pwd != oldpwd)
                {
                    return RespResult.Faild("旧密码错误");
                }
                if (newpwd.Length < 6 || newpwd != newpwd2)
                {
                    return RespResult.Faild("新密码输入不正确");
                }
                EFYihongIotContext.bs_client_user.Where(c => c.cu_guid == user.cu_guid).ExecuteUpdate(c => c.SetProperty(p => p.cu_pwd, newpwd)
                .SetProperty(p => p.cu_mod_time, DateTime.Now));
                return RespResult.Sucess();
            }
            return RespResult.Faild();
            #endregion
        }


        public IActionResult Right()
        {
            return View();
        }
        public IActionResult VoiceExplain()
        {
            return View();
        }
        public IActionResult test()
        {
            return View();
        }
    }
}